/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ensisa.sunspot.event;

import java.io.Serializable;

/**
 *
 * @author michel
 */
public abstract class AbstractEvent implements IEvent, Serializable {
	private static final long serialVersionUID = -6375101358018851388L;
	
	private long timeStamp;
    private int state;
    private int major;
    private int minor;
    private String source;

    public AbstractEvent (int major, int minor) {
        super ();
        this.major = major;
        this.minor = minor;
        this.state = STATE_RELEASED;
        this.source = UNKNOWN_SOURCE;
    }

    public long getTimeStamp () {
        return timeStamp;
    }

    public void setTimeStamp (long timeStamp) {
        this.timeStamp = timeStamp;
    }

    public int getState () {
        return state & FIELD_STATE;
    }

    public boolean isEmpty () {
        return getState() == STATE_EMPTY;
    }

    public boolean isRaw() {
        return getState() == STATE_RAW;
    }

    public boolean isReleased () {
        return getState() == STATE_RELEASED;
    }

    public boolean isStarted () {
        return getState() == STATE_STARTED;
    }

    public boolean isPreStarted () {
        return getState() == STATE_PRE_STARTED;
    }

    public boolean isRepeated () {
        return getState() == STATE_REPEATED;
    }

    public boolean isPreEnded () {
        return getState() == STATE_PRE_ENDED;
    }

    public boolean isEnded () {
        return getState() == STATE_ENDED;
    }

    public boolean isPre () {
        if (getState() == STATE_PRE_STARTED) return true;
        if (getState() == STATE_PRE_ENDED) return true;
        return false;
    }

    public void setState (int state) {
        this.state = state & FIELD_STATE;
    }

    public int getMajor () {
        return major & FIELD_MAJOR;
    }

    public void setMajor (int major) {
        this.major = major & FIELD_MAJOR;
    }

    public boolean isAcceleration () {
        return getMajor() == MAJOR_ACCELERATION;
    }

    public boolean isTilt () {
        return getMajor() == MAJOR_TILT;
    }

    public boolean isSwitch () {
        return getMajor() == MAJOR_SWITCH;
    }

    public boolean isLight () {
        return getMajor() == MAJOR_LIGHT;
    }

    public boolean isTemperature () {
        return getMajor() == MAJOR_TEMPERATURE;
    }

    public boolean isRadio () {
        return getMajor() == MAJOR_RADIO;
    }

    public int getMinor () {
        return minor & FIELD_MINOR;
    }

    public void setMinor (int minor) {
        this.minor = minor & FIELD_MINOR;
    }

    public boolean isX () {
        return (getMinor() & MINOR_X) == MINOR_X;
    }

    public boolean isY () {
        return (getMinor() & MINOR_Y) == MINOR_Y;
    }

    public boolean isZ () {
        return (getMinor() & MINOR_Z) == MINOR_Z;
    }

    public boolean isLeft () {
        return (getMinor() & MINOR_LEFT ) == MINOR_LEFT;
    }

    public boolean isRight () {
        return (getMinor() & MINOR_RIGHT) == MINOR_RIGHT;
    }

    public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String toString () {
        StringBuffer tmp = new StringBuffer ();
        if (timeStamp != 0) {
            tmp.append("@");
            tmp.append(timeStamp);
        }
        tmp.append(" ");
        tmp.append (stateInText(getState()));
        tmp.append(" ");
        tmp.append (majorInText(getMajor()));
        tmp.append(" ");
        tmp.append (minorInText(getMajor(), getMinor()));
        tmp.append(" ");
        tmp.append(source);
        return tmp.toString();
    }

    static public final String stateInText (int state) {
        switch (state) {
        case IEvent.STATE_EMPTY       : return "empty";
        case IEvent.STATE_RELEASED    : return "released";
        case IEvent.STATE_PRE_STARTED : return "pre started";
        case IEvent.STATE_STARTED     : return "started";
        case IEvent.STATE_REPEATED    : return "repeated";
        case IEvent.STATE_PRE_ENDED   : return "pre ended";
        case IEvent.STATE_ENDED       : return "ended";
        default                           : return "???";
        }
    }

    static public final String majorInText (int major) {
        switch (major) {
        case IEvent.MAJOR_ACCELERATION    : return "acceleration";
        case IEvent.MAJOR_TILT            : return "tilt";
        case IEvent.MAJOR_SWITCH          : return "switch";
        case IEvent.MAJOR_LIGHT           : return "light";
        case IEvent.MAJOR_TEMPERATURE     : return "temperature";
        case IEvent.MAJOR_RADIO           : return "radio";
        default                           : return "???";
        }
    }

    static public final String minorInText (int major, int minor) {
        StringBuffer tmp = new StringBuffer ();
        if ((major & (MAJOR_ACCELERATION | MAJOR_TILT)) != 0) {
            if ((minor & MINOR_X) == MINOR_X) tmp.append(" X ");
            if ((minor & MINOR_Y) == MINOR_Y) tmp.append(" Y ");
            if ((minor & MINOR_Z) == MINOR_Z) tmp.append(" Z ");
        }
        if ((minor & MINOR_X) == MINOR_X) {
            if ((minor & MINOR_LEFT) == MINOR_LEFT) tmp.append(" L ");
            if ((minor & MINOR_RIGHT) == MINOR_RIGHT) tmp.append(" R ");
        }
        return tmp.toString();
    }
    
    public IEvent clone() {
    	return null;
    }

}
